package com.maimang.platform.base;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.maimang.platform.common.entity.SysUser;
import com.maimang.platform.common.utils.DateUtils;
import com.maimang.platform.common.utils.ShiroUtils;
import com.maimang.platform.lang.StringUtils;
import com.maimang.platform.page.DataInfo;
import com.maimang.platform.page.PageDomain;
import com.maimang.platform.page.TableDataInfo;
import com.maimang.platform.page.TableSupport;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.beans.PropertyEditorSupport;
import java.util.Date;
import java.util.List;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname BaseController
 * @description web层通用数据处理
 * @date 2019-04-16 20:04
 * @email jintaozhao@qq.com
 */
public class BaseController {
	/**
	 * 将前台传递过来的日期格式的字符串，自动转化为Date类型
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		// Date 类型转换
		binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
			@Override
			public void setAsText(String text) {
				setValue(DateUtils.parseDate(text));
			}
		});
	}

	/**
	 * 设置请求分页数据
	 */
	protected void startPage() {
		PageDomain pageDomain = TableSupport.initPageRequest();
		Integer pageNum = pageDomain.getPageNum();
		Integer pageSize = pageDomain.getPageSize();
		if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
			String orderBy = pageDomain.getOrderBy();
			PageHelper.startPage(pageNum, pageSize, orderBy);
		}
	}

	/**
	 * 获取前台传递的分页数据
	 *
	 * @return 分页数据对象
	 */
	protected PageDomain getPageDomain() {
		PageDomain pageDomain = TableSupport.initPageRequest();
		return pageDomain;
	}

	/**
	 * 响应请求分页数据
	 */
	protected TableDataInfo getDataTable(List<?> list) {
		TableDataInfo rspData = new TableDataInfo();
		rspData.setCode(200);
		rspData.setRows(list);
		long total = new PageInfo(list).getTotal();
		rspData.setTotal(total);
		PageDomain pageDomain = TableSupport.initPageRequest();
		if (pageDomain != null) {
			int pageNum = pageDomain.getPageNum() == null ? 1 : pageDomain.getPageNum();
			int pageSize = pageDomain.getPageSize() == null ? 10 : pageDomain.getPageSize();
			rspData.setCurrentPage(pageNum);
			long totalPage = ((long) Math.ceil((double) total / pageSize));
			long nextPage = (pageNum + 1 > totalPage) ? totalPage : (pageNum + 1);
			long prePage = (pageNum - 1 < 0) ? pageNum : (pageNum - 1);
			rspData.setTotalPage(totalPage);
			rspData.setNextPage(nextPage);
			rspData.setPrePage(prePage);
		}
		return rspData;
	}

	protected TableDataInfo getDataTable1(List<?> list) {
		TableDataInfo rspData = new TableDataInfo();
		rspData.setCode(200);
		PageDomain pageDomain = TableSupport.initPageRequest();
		int beginIndex = (pageDomain.getPageNum() - 1) * pageDomain.getPageSize();
		int endIndex = Math.min(pageDomain.getPageNum() * pageDomain.getPageSize(), list.size());
		List<?> target = list.subList(beginIndex, endIndex);
		long total = new PageInfo(list).getTotal();
		rspData.setRows(target);
		rspData.setTotal(total);
		if (pageDomain != null) {
			int pageNum = pageDomain.getPageNum() == null ? 1 : pageDomain.getPageNum();
			int pageSize = pageDomain.getPageSize() == null ? 10 : pageDomain.getPageSize();
			rspData.setCurrentPage(pageNum);
			long totalPage = total / pageSize + 1L;
			long nextPage = (pageNum + 1 > totalPage) ? totalPage : (pageNum + 1);
			long prePage = (pageNum - 1 < 0) ? pageNum : (pageNum - 1);
			rspData.setTotalPage(totalPage);
			rspData.setNextPage(nextPage);
			rspData.setPrePage(prePage);
		}
		return rspData;
	}

	/**
	 * 响应请求分页数据
	 */
	protected DataInfo getDataInfo(List<?> list) {
		DataInfo rspData = new DataInfo();
		rspData.setRows(list);
		long total = new PageInfo(list).getTotal();
		rspData.setTotal(total);
		PageDomain pageDomain = TableSupport.initPageRequest();
		if (pageDomain != null) {
			int pageNum = pageDomain.getPageNum() == null ? 1 : pageDomain.getPageNum();
			int pageSize = pageDomain.getPageSize() == null ? 10 : pageDomain.getPageSize();
			rspData.setCurrentPage(pageNum);
			long totalPage = total / pageSize + 1L;
			long nextPage = (pageNum + 1 > totalPage) ? totalPage : (pageNum + 1);
			long prePage = (pageNum - 1 < 0) ? pageNum : (pageNum - 1);
			rspData.setTotalPage(totalPage);
			rspData.setNextPage(nextPage);
			rspData.setPrePage(prePage);
		}
		return rspData;
	}


	/**
	 * 响应返回结果
	 *
	 * @param rows 影响行数
	 * @return 操作结果
	 */
	protected Result toResult(int rows) {
		return rows > 0 ? success() : error();
	}

	/**
	 * 响应返回结果
	 *
	 * @param result 结果
	 * @return 操作结果
	 */
	protected Result toResult(boolean result) {
		return result ? success() : error();
	}

	/**
	 * 返回成功
	 */
	public Result success() {
		return Result.SUCCESS(200, "操作成功");
	}

	/**
	 * 返回失败消息
	 */
	public Result error() {
		return Result.FAIL(500, "操作失败");
	}

	/**
	 * 返回成功消息
	 */
	public Result success(String message) {
		return Result.SUCCESS(200, message);
	}

	/**
	 * 返回失败消息
	 */
	public Result error(String message) {
		return Result.FAIL(500, message);
	}

	/**
	 * 返回错误码消息
	 */
	public Result error(int code, String message) {
		return Result.FAIL(code, message);
	}

	/**
	 * 页面跳转
	 */
	public String redirect(String url) {
		return StringUtils.format("redirect:{}", url);
	}

	/**
	 * 获取当前登录用户
	 *
	 * @return 登录用户
	 */
	public SysUser getSysUser() {
		return ShiroUtils.getSysUser();
	}

	public void setSysUser(SysUser user) {
		ShiroUtils.setSysUser(user);
	}

	public Long getUserId() {
		return getSysUser().getUserId();
	}

	/**
	 * 获取当前登录用户名
	 *
	 * @return 当前登录名称
	 */
	public String getLoginName() {
		return getSysUser().getLoginName();
	}
}
